<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/18/0018
 * Time: 15:33
 */

namespace app\sports_admin\service;


use app\exception\ExpensesReceiptsException;
use think\Request;
use think\Session;
use app\sports_admin\model\ExpensesReceipts as ExpensesReceiptsModel;
class ExpensesReceipts
{
    /*
     * 收支单添加
     */
    public static function insert()
    {
        $params = self::getParams();
        $params['operator'] = Session::get('sport_admin.name');
        $params['status'] = Session::get('sport_admin.scope') == 128 ? 2 : 1;
        $bool = ExpensesReceiptsModel::create($params);
        if(!$bool){
            throw new ExpensesReceiptsException([
                'errorCode'=>400,
                'msg'=>'添加失败'
            ]);
        }
        return json(['code'=>200,'msg'=>'添加成功']);
    }
    /*
     * 收支单修改
     * @param $id 收支信息id
     */
    public static function update()
    {
        $params = self::getParams();
        $id = intval(input('post.id'));
        $bool = (new ExpensesReceiptsModel())->save($params,['id'=>$id]);
        if(!$bool){
            throw new ExpensesReceiptsException([
                'errorCode'=>400,
                'msg'=>'修改失败'
            ]);
        }
        return json(['code'=>200,'msg'=>'修改成功']);
    }
    /*
     * 审核通过
     * @param $id 收支信息id
     */
    public static function adopt()
    {
        $id = intval(input('post.id'));
        $bool = (new ExpensesReceiptsModel())->save(['status'=>2],['id'=>$id]);
        if(!$bool){
            throw new ExpensesReceiptsException([
                'errorCode'=>400,
                'msg'=>'审核失败'
            ]);
        }
        return json(['code'=>200,'msg'=>'审核成功']);
    }
    /*
     * 获取收支单信息
     */
    private static function getParams()
    {
        // applicant 申请人  money 金额  type 1/2 收入/支出 remarks 备注
        return Request::instance()->only(['applicant','money','type','remarks']);
    }
}